/*!
 * Util JS
 *
 * 存储项目工具js代码
 *
 * @author ZhaoYimeng
 * @version 1.0.0 20180820
 */
import Vue from 'vue'
import {
  Indicator
} from 'mint-ui';

/**
 * network工具实例
 */
const NetworkUtil = new class {
  /**
   * 使用本地缓存的异步网络请求
   *
   * @param cacheName 本地缓存名称,必须全局唯一
   * @param cacheTime 本地缓存时间,单位毫秒
   * @param settings 请求参数对象
   * @param isNow 是否立即请求(true: 立即请求)
   * @param successCallback 异步处理成功回调函数
   * @param failedCallback 异步处理失败回调函数
   */
  resourceAndCache(cacheName, cacheTime, settings, isNow, successCallback, failedCallback) {
    var data = sessionStorage.getItem(cacheName);
    if (data && !isNow) {
      data = JSON.parse(data);
      if (parseInt(data.expiration) - new Date().getTime() > 3000) {
        if (successCallback) successCallback(data.data);
        return;
      }
    }
    Indicator.open({
      text: '',
      spinnerType: 'fading-circle'
    });
    Vue.http[settings.method](settings.url, settings.body).then(response => {
      Indicator.close();
      if (0 > cacheTime) cacheTime = 3600000;
      var result = {
        data: response.body,
        expiration: new Date().getTime() + cacheTime
      };
      sessionStorage.setItem(cacheName, JSON.stringify(result));
      if (successCallback) successCallback(response.body);
    }, (err) => {
      Indicator.close();
      if (failedCallback) failedCallback(err)
    })
  }

  /**
   * 网络请求
   *
   * @param settings 请求参数
   * @param successCallback 成功回调函数
   * @param cacheParams 缓存对象(非必传){cacheName: 缓存名称, cacheTime: 缓存时间, isNow: 是否立即重新请求}
   * @param failedCallback 失败回调函数(非必传)
   */
  request(settings, successCallback, failedCallback, cacheParams) {
    // if (process.env.NODE_ENV === 'production') settings.url = settings.url.substring(4, settings.url.length)
    // if (settings.method === 'get') {
    if (cacheParams) {
      this.resourceAndCache(cacheParams.cacheName, cacheParams.cacheTime, settings, cacheParams.isNow, successCallback, failedCallback);

      return;
    }
    // }
    Indicator.open({
      text: '',
      spinnerType: 'fading-circle'
    });
    Vue.http[settings.method](settings.url, settings.body).then(response => {
      Indicator.close();
      if (successCallback) {
        successCallback(response.body);
      }
    }, (err) => {
      Indicator.close();
      if (failedCallback) failedCallback(err)
    })
  }
}();

/**
 * MintUI工具实例
 */
const MintUIUtil = new class {
  /**
   * Toast
   *
   * @param message 提示信息
   * @param operateName 操作名称(success || !success)
   */
  toast(message, operateName) {
    let operate = false
    if (operateName === 'success') operate = true
    let className = operate ? 'humi-toast humi-toast-success' : 'humi-toast'
    Vue.$toast({
      message: message,
      position: 'bottom',
      duration: 2000,
      className: className
    })
  }
}();


/**
 * 截取定长字符串后添加...
 *
 * @param str 待操作字符串
 * @param length 截取长度
 */
const stringSplice = (str, length) => {
  if (str.length > length) str = str.substring(0, length) + '...'
  return str
}

export {
  NetworkUtil,
  MintUIUtil,
  stringSplice
};